package com.webermagem.util;

import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

public class SessionManager {
	
	@SuppressWarnings("rawtypes")
	public static Object getSessionAttribute(FacesContext facesContext, String attributeName) {  
		try {  
			ExternalContext ec = getExternalContext();  
		    if (ec != null){  
		        Map attrMap = ec.getSessionMap();     
		        if (attrMap != null) {  
		            return attrMap.get(attributeName);  
		        } else {  
		            return null;  
		        }  
		    } else {  
		        return null;  
		    }  
		} catch (Exception e) {  
		    e.printStackTrace();  
		    return null;  
		}  
		}  

		@SuppressWarnings({ "unchecked", "rawtypes" })  
		public static void setSessionAttribute(FacesContext facesContext, String attributeName, Object attributeValue) {  
		try {  
		    ExternalContext ec = getExternalContext();  
		    if (ec != null){  
		        Map attrMap = ec.getSessionMap();     
		        if (attrMap != null) {  
		            attrMap.put(attributeName, attributeValue);  
		        }   
		    }   
		} catch (Exception e) {  
		    e.printStackTrace();  
		}  
		}  

		public static ExternalContext getExternalContext() {  
		try {  
		    FacesContext facesContext = FacesContext.getCurrentInstance();  
		    if (facesContext == null) {  
		        return null;  
		    } else {  
		        return facesContext.getExternalContext();  
		    }  
		} catch (Exception e) {  
		    e.printStackTrace();  
		    return null;  
		   }      
		}  
	
}
